<?php

App::uses('CrudController', 'Controller');
App::uses('Certification', 'Model');
App::uses('Workstation', 'Model');
App::uses('Categorization', 'Model');

class CertificationsController extends CrudController {

    public $_model = 'Certification';

    public function index($line = null) {
        $this->request->allowMethod('get');
        $appBosch = new stdClass();
        $appBosch->line = $line;
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Mis Certificaciones'));
        $this->set('description', __('Vigentes'));
    }

    public function getDetails($certId) {
        $this->request->allowMethod('get');
        $this->layout = null;
        $this->loadModel('CertificationDetail');
        $records = $this->CertificationDetail->getByCertification($certId, $this->Auth->user('id'));
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function certificate($idCertification) {
        $this->loadModel('Certificate');
        $res = $this->Certificate->findByCertificationId($idCertification);
        $nRecord = $res['Certificate'];
        $nRecord['data'] = json_decode($nRecord['data']);
        $this->set('record', $nRecord);
        $appBosch = new stdClass();
        $this->set('appBosch', $appBosch);
    }

    public function getLast() {
        $this->loadModel('LogCertification');
        $userSessionId = $this->Auth->user('id');
        $appBosch = new stdClass();
        $appBosch->records = $this->LogCertification->getCreateCertificationsByUser($userSessionId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Certificaciones'));
        $this->set('description', __('Creadas'));
    }

    public function getLastDestroy() {
        $this->loadModel('LogCertification');
        $userSessionId = $this->Auth->user('id');
        $appBosch = new stdClass();
        $appBosch->records = $this->LogCertification->getDestroyCertificationsByUser($userSessionId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Certificaciones'));
        $this->set('description', __('Eliminadas'));
    }

    public function processCreate() {
        $this->loadModel('User');
        $appBosch = new stdClass();
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Crear certificaciones'));
    }

    public function generateCertificates() {
        $this->loadModel('Certificate');
        $m = $this->_model;
        $certs = $this->$m->getInfoCertificates();
        $certificates = array();
        foreach ($certs as $cert) {
            $certificates[] = array(
                'certification_id' => $cert['idCert'],
                'data' => json_encode(array(
                    'name' => $cert['nameU'],
                    'workstation' => $cert['nameW'],
                    'img' => $cert['img'],
                    'rh_sign' => $cert['rh_sign'],
                    'rh_job' => $cert['rh_job'],
                    'rh_name' => $cert['rh_name'],
                    'moe_sign' => $cert['moe_sign'],
                    'moe_job' => $cert['moe_job'],
                    'moe_name' => $cert['moe_name'],
                    'ts_sign' => $cert['ts_sign'],
                    'ts_job' => $cert['ts_job'],
                    'ts_name' => $cert['ts_name']
                )),
                'date' => $cert['date']
            );
        }
        $this->Certificate->saveMany($certificates);
    }

    public function admin() {
        $this->request->allowMethod('get');
        $appBosch = new stdClass();
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Certificaciones'));
        $this->set('description', __('Modificación de vigencia'));
    }

    public function getNextToExpire() {
        $m = $this->_model;
        $userId = $this->Auth->user('id');
        $records = $this->$m->getEnabledNextToExpireByUser($userId);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function updateDateExpire() {
        $this->request->allowMethod('get');
        $records = "error";
        $permissions = $this->getPermissionsCertifications();
        if ($permissions->haveModifyLive()) {
            $this->loadModel('LogCertification');
            $cert = $this->request->query['models'];
            $cert = json_decode($cert);
            $m = $this->_model;
            $certUp = array();
            $certUp['id'] = $cert->value;
            $certUp['life'] = $cert->end;
            $records = false;
            if ($this->$m->save($certUp)) {
                $this->$m->clear();
                $this->$m->id = $cert->value;
                $reg = $this->$m->read();
                $records = $cert;
                $this->LogCertification->addUpdateLog($this->Auth->user('id'), $reg[$m], 'Modificación de vigencia');
            }
        }
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($param = null)
    {
        $m = $this->_model;
        $user = $this->Auth->user();
        $records = $this->$m->getEnabled($user['id'], $param);
        return $records;
    }

    protected function c($model)
    {
        return array(
            'text' => $model->text,
            'name' => $model->text,
        );
    }

    protected function u($model)
    {
        return array(
            'text' => $model->text,
            'name' => $model->text,
        );
    }

    protected function id($model)
    {
        return $model->value;
    }
}
